package com.ita.acmsystem.merchant.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ita.acmsystem.merchant.entity.FoodTypes;
import com.ita.acmsystem.merchant.entity.Foods;
import com.ita.acmsystem.merchant.entity.Merchant;
import com.ita.acmsystem.merchant.service.IFoodService;
import com.ita.acmsystem.merchant.service.IFoodTypesService;
import com.ita.acmsystem.merchant.service.IMerchantService;
import com.ita.acmsystem.merchant.service.impl.FoodServiceImpl;
import com.ita.acmsystem.merchant.service.impl.FoodTypesServiceImpl;
import com.ita.acmsystem.merchant.service.impl.MerchantServiceImpl;

/**
 * Servlet implementation class UpdateContentServlet
 */
public class UpdateFoodsContentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private IFoodService foodService = new FoodServiceImpl();
	private IFoodTypesService foodTypesService = new FoodTypesServiceImpl();
	private IMerchantService merchantService = new MerchantServiceImpl();
	
	List<FoodTypes> foodTypesList;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateFoodsContentServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		updateContentServlet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		updateContentServlet(request, response);
	}

	private void updateContentServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String message = request.getParameter("message");
		System.out.println("UpdateFoodsContentServlet" + message);
		String foodId = request.getParameter("foodId");
		Foods food = foodService.getFoodsById(foodId);
		
		if (foodTypesList == null) {
			foodTypesList = foodTypesService.getAllFoodTypes();
		}
		
		Merchant merchant = merchantService.getMerchantById(food.getMerchantId());
		
		request.setAttribute("message", message);
		request.setAttribute("food", food);
		request.setAttribute("foodTypesList", foodTypesList);
		request.setAttribute("merchant", merchant);
		request.getRequestDispatcher("update.jsp").forward(request, response);
	}

}
